<%@ page language="java" contentType="text/html" pageEncoding="utf-8" %>
<%@page import="java.io.*,java.util.*,java.sql.*,com.animal.dao.*" %>
<%@ page import="com.utils.JdbcUtils" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>LoginCheck</title></head>
<body>

<%
    String id = request.getParameter("ID");
    String password = request.getParameter("Password");
    //获取类型
    String type = request.getParameter("type");
    //System.out.println(type);

    //Cookie传值
    Cookie c = new Cookie("id", id);
    c.setMaxAge(-1);
    response.addCookie(c);

    //session传值
    session.setAttribute("ID", id);

    //request传值
    request.getSession().setAttribute("uid", id);
    request.getSession().setAttribute("upw", password);

    //判断类型
    //管理员
    if ("1".equals(type)) {

        String queryStr = "select Position from animalfile09_archvist where ID=? and Password=?";

        Connection conn = Connect.getCon();
        PreparedStatement pstmt = conn.prepareStatement(queryStr);
        ResultSet rs = null;

        pstmt.setString(1, id);
        pstmt.setString(2, password);
        rs = pstmt.executeQuery();

        if (rs.next()) {
            String pos = rs.getString(1);  //职位
            //日常数据管理员
            if ("daily".equals(pos)) {
%>
<%-- <h4>管理员登录成功,欢迎您!    <%=id %></h4> --%>
<script type="text/javascript">
    window.location.href = "DailyFileManager.html";

    //window.location.href="Manage.html";
</script>
<%
}
//实验数据管理员
else if ("experimental".equals(pos)) {
%>
<%-- <h4>管理员登录成功,欢迎您!    <%=id %></h4> --%>
<script type="text/javascript">
    window.location.href = "ExperimentalFileManager.html";

    //window.location.href="Manage.html";
</script>
<%
}
//纸质档案管理员
else if ("paper".equals(pos)) {
%>
<%-- <h4>管理员登录成功,欢迎您!    <%=id %></h4> --%>
<script type="text/javascript">
    window.location.href = "PaperFileManager.html";

    //window.location.href="Manage.html";
</script>
<%
} else {   //没有职位
%>
<script type="text/javascript" language="javascript">
    alert("登录失败！请输入正确的用户名或密码。");
    window.document.location.href = "login.jsp";
</script>
<!-- <center>
<h4>登录失败！请输入正确的用户名或密码。</h4>
<a href="login.jsp">重新登录</a>
</center> -->
<%
        //out.print("登录失败 ,"+username+"! 请输入正确的用户名或密码!");
    }
} else {  //没有该用户
%>
<script type="text/javascript" language="javascript">
    alert("登录失败！请输入正确的用户名或密码。");
    window.document.location.href = "login.jsp";
</script>
<!-- <center>
<h4>登录失败！请输入正确的用户名或密码。</h4>
<a href="login.jsp">重新登录</a>
</center> -->
<%
        //out.print("登录失败 ,"+username+"! 请输入正确的用户名或密码!");
    }

    rs.close();
    pstmt.close();
    conn.close();
}

//普通用户
else {
    //request.getRequestDispatcher("/common.jsp").forward(request, response);
    // 连接数据库
    String queryStr = "select * from animalfile09_systemuser where UserID=? and Password=?";
    Connection conn = JdbcUtils.getCon();
    PreparedStatement pstmt = conn.prepareStatement(queryStr);
    ResultSet rs = null;

    pstmt.setString(1, id);
    pstmt.setString(2, password);
    rs = pstmt.executeQuery();

    if (rs.next()) {
        String name = rs.getString("Name");
        if (name != null) {

            session.setAttribute("id", id);
            session.setAttribute("name", name);
%>
<script type="text/javascript">
    window.location.href = "CustomerManage.html";
</script>
<%
} else {
%>
<center>
    <h4>用户登录失败！请输入正确的用户名或密码。</h4>
    <a href="login.jsp">重新登录</a>
</center>
<%
    }
} else {
%>
<center>
    <h4>管理员登录失败！请输入正确的用户名或密码。</h4>
    <a href="login.jsp">重新登录</a>
</center>
<%
            //out.print("登录失败 ,"+username+"! 请输入正确的用户名或密码!");
        }
        rs.close();
        pstmt.close();
        conn.close();
    }
%>

</body>
</html>